<?php
declare (strict_types=1);

namespace app\api\validate;

use app\common\model\User as UserModel;
use think\Validate;

class Register extends Validate
{
    protected $rule = [
        "mobile" => "require|mobile|checkOnly:mobile",
        "nickname" => "require",
        "password" => "require|min:6",
        "pay_password" => "require|min:6",
        "parent_code" => "require",
        "agree" => "require"
    ];

    protected $message = [
        'mobile.require' => '请输入手机号',
        'mobile.mobile' => '手机号格式有误',
        'mobile.checkOnly' => '手机号已存在',
        'nickname.require' => '请输入昵称',
        'password.require' => '请输入登录密码',
        'password.min' => '登录密码至少六位',
        'pay_password.require' => '请输入支付密码',
        'pay_password.min' => '支付密码至少六位',
        'parent_code.require' => '请输入邀请码',
        'agree.require' => '请您阅读并同意《注册协议》和《隐私协议》'
    ];

    protected function checkOnly($value, $rule, $data = []): bool
    {
        $that = UserModel::where([$rule => $value]);
        if (!empty($data['id'])) {
            $that->where('id', '<>', $data['id']);
        }
        $count = $that->count();

        return $count ? false : true;
    }
}